﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace QLNSClient
{
    public partial class uc_qdChucvu : UserControl
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public uc_qdChucvu()
        {
            InitializeComponent();
        }


        //-----------ham ma hoa anh sang ma nhi phan--------------------

        public static Byte[] ReadImageFromFile(string ImagePath)
        {
            Byte[] ImageData = null;  //khai bao mang ImageData kieu byte luu tru anh
            FileInfo fi = new FileInfo(ImagePath); //      khai bao bien fi kieu FileInfo de doc thong tin ImagePath
            long NumberOfBytes = fi.Length; // khai bao bien NumberOfBytes kieu Long de luu do dai cua bien fi
            FileStream fs = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);         // bien br doc bien fs
            ImageData = br.ReadBytes((int)NumberOfBytes);
            return ImageData;
        }

        public void LoadData()
        {
            lke_MaCB.EditValue = FrmMain.sendMa_CB_CBCV;

            btn_Them.Enabled = false;
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

          
            lke_MaCB.Enabled = false;
            lke_Chucvu.Enabled = true;
            lke_DonviBonhiem.Enabled = true;
            txt_Quyetdinh.Enabled = true;
            de_Ngayky.Enabled = true;
            buttonEditFileQD.Enabled = true;

            

            lke_MaCB.Properties.DataSource = sv.getCB();

            lke_Chucvu.Properties.DataSource = sv.getCV();

            lke_DonviBonhiem.Properties.DataSource = sv.getDMDV();

            gv_dsCBCV.DataSource = sv.getCBCV();
            
            
            gridView1.Columns[0].Caption = "Mã cán bộ";
            gridView1.Columns[1].Caption = "Mã chức vụ";
            gridView1.Columns[2].Caption = "Mã đơn vị";
            gridView1.Columns[3].Caption = "Quyết định số";
            gridView1.Columns[4].Caption = "Ngày ký";
            gridView1.Columns[5].Visible = false;
            gridView1.Columns[6].Visible = false;

            //for (int i = 0; i < gridView1.RowCount; i++)
            //{
            //    if (gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() == FrmMain.sendMa_CB_CBCV)
            //    {
            //        lke_Chucvu.EditValue = gridView1.GetRowCellValue(i, gridView1.Columns[1]).ToString();
            //        lke_DonviBonhiem.EditValue = gridView1.GetRowCellValue(i, gridView1.Columns[2]).ToString();
            //        txt_Quyetdinh.Text = gridView1.GetRowCellValue(i, gridView1.Columns[3]).ToString();
            //        de_Ngayky.DateTime = DateTime.Parse(gridView1.GetRowCellValue(i, gridView1.Columns[4]).ToString());
            //    }
            //}
                //List<QLNSServiceReference.DANHMUC_DONVI> listTenDVtheoMaCB = new List<QLNSServiceReference.DANHMUC_DONVI>(sv.getTenDVtheoMaCB(lke_MaCB.EditValue.ToString()));
                //string a = listTenDVtheoMaCB[0].Ma_DV;
                //lke_dmDonvi.EditValue = a;
            if(gridView1.DataRowCount<=0)
            {
                MessageBox.Show("Không có dữ liệu!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void uc_qdChucvu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            lke_Chucvu.Enabled = true;
            lke_DonviBonhiem.Enabled = true;
            txt_Quyetdinh.Enabled = true;
            de_Ngayky.Enabled = true;
            buttonEditFileQD.Enabled = true;
            buttonEditFileQD.Text = null;
    
            lke_Chucvu.EditValue = "";
            lke_DonviBonhiem.EditValue = "";
            txt_Quyetdinh.Text = "";
            de_Ngayky.Text = "";
           
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            //string tempDonvi = lke_dmDonvi.EditValue.ToString();
            btn_Huy.Enabled = false;
            //btn_Them.Enabled = true;


            string tempMaCB="";
            string tempMaCV="";
            string tempMaDV="";
            if (lke_MaCB.EditValue.ToString() == "")
            {
                MessageBox.Show("Bạn chưa chọn Cán bộ!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tempMaCB = null;
                lke_MaCB.Focus();
            }
            else
            {
                tempMaCB=lke_MaCB.EditValue.ToString();
            }

            if (lke_Chucvu.EditValue.ToString() == "")
            {
                MessageBox.Show("Bạn chưa chọn Chức vụ!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tempMaCV = null;
                lke_Chucvu.Focus();
            }
            else
            {
                tempMaCV = lke_Chucvu.EditValue.ToString();
            }

            if (lke_DonviBonhiem.EditValue.ToString() == "")
            {
                MessageBox.Show("Bạn chưa chọn Đơn vị cần bổ nhiệm!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lke_DonviBonhiem.Focus();
                tempMaDV = null;
            }
            else
            {
                tempMaDV=lke_DonviBonhiem.EditValue.ToString();
            }
                    string tempQuyetdinh = txt_Quyetdinh.Text;

                    DateTime tempNgayky;
                    if (de_Ngayky.Text == "")
                    {
                        tempNgayky = DateTime.Today.Date;
                    }
                    else
                    {
                        tempNgayky = de_Ngayky.DateTime;
                    }

                    QLNSServiceReference.CANBO_CHUCVU cbcv = new QLNSServiceReference.CANBO_CHUCVU();


                    if (tempMaCB != null && tempMaCV != null && tempMaDV != null)
                    {
                        cbcv.Ma_CB = tempMaCB;
                        cbcv.Ma_CV = tempMaCV;
                        cbcv.Ma_DV = tempMaDV;

                        cbcv.QuyetDinh = tempQuyetdinh;
                        cbcv.NgayKy = tempNgayky;
                        if (buttonEditFileQD.Text.Trim() != "")
                        {
                            FileStream frm;
                            frm = new FileStream(@buttonEditFileQD.Text.Trim(), FileMode.Open, FileAccess.Read);
                            byte[] picbyte = new byte[frm.Length];
                            frm.Read(picbyte, 0, System.Convert.ToInt32(frm.Length));
                            frm.Close();
                            frm.Dispose();
                            cbcv.FileQuyetDinh = picbyte;

                        }
                        else
                        {
                            byte[] picbyte = new byte[0];
                            cbcv.FileQuyetDinh = picbyte;

                        }
                        if (demsua == 1)
                        {
                            DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật thông tin chức vụ '" + lke_Chucvu.Text + "' của cán bộ '" + lke_MaCB.Text + "' tại đơn vị '" + lke_DonviBonhiem.Text + "'?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                            if (dr == DialogResult.OK)
                            {
                                sv.updateCBCV(cbcv);
                                LoadData();
                                demsua = 0;
                                MessageBox.Show("Cập nhật thông tin thành công!");
                            }
                        }
                        else
                        {
                            int kq = sv.addCBCV(cbcv);
                            if (kq != -1)
                            {
                                LoadData();
                                MessageBox.Show("Thêm thông tin thành công!");

                            }
                            else
                            {
                                MessageBox.Show("Chức vụ cho đơn vị này đã tồn tại! Vui lòng xem lại!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                    }
                
           
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            //btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
            if (gridView1.DataRowCount > 0)
            {
                DialogResult dr = MessageBox.Show("Bạn có chắc muốn hủy chức vụ '" + lke_Chucvu.Text + "' của cán bộ '" + lke_MaCB.Text + "' tại đơn vị '" + lke_DonviBonhiem.Text + "'?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sv.deleteCBCV(macb, macv, madvbn);
                    LoadData();
                    MessageBox.Show("Hủy chức vụ thành công!");
                }
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để xóa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                demsua = 1;

                btn_Luu.Enabled = true;
                btn_Huy.Enabled = true;
                btn_Them.Enabled = false;
                btn_Sua.Enabled = false;

                txt_Quyetdinh.Enabled = true;
                de_Ngayky.Enabled = true;
                buttonEditFileQD.Enabled = true;
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để sửa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }


        //string tempPath;
        //private void btn_themAnh_Click(object sender, EventArgs e)
        //{
        //    OpenFileDialog op = new OpenFileDialog();
        //    op.Filter = "JPG|*.JPG|PNG|*.PNG|BITMAP|*.BMP";
        //    if (op.ShowDialog() == DialogResult.OK)
        //    {
                
        //        tempPath = op.FileName;
        //    }
        //}

        private void lke_dmDonvi_EditValueChanged(object sender, EventArgs e)
        {
            //string a = lke_dmDonvi.EditValue.ToString();
            //lke_MaCB.Properties.DataSource = sv.getCBtheoDV(a);
        }


        string macb="";
        string macv="";
        string madvbn="";
        private void gv_dsCBCV_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;

            //lke_dmDonvi.Enabled = false;
            lke_MaCB.Enabled = false;
            lke_Chucvu.Enabled = false;
            lke_DonviBonhiem.Enabled = false;
            txt_Quyetdinh.Enabled = false;
            de_Ngayky.Enabled = false;
            buttonEditFileQD.Enabled = false;          

            try
            {

                lke_MaCB.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                lke_Chucvu.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                lke_DonviBonhiem.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                txt_Quyetdinh.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
                de_Ngayky.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[4]));


                macb = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                macv = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                madvbn = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);

                //MessageBox.Show(macb + macv + madvbn);
                //----------------------lay ten don vi----------------------------

                List<QLNSServiceReference.DANHMUC_DONVI> listTenDVtheoMaCB = new List<QLNSServiceReference.DANHMUC_DONVI>(sv.getTenDVtheoMaCB(macb));
                string a = listTenDVtheoMaCB[0].Ma_DV;
                //lke_dmDonvi.EditValue = a;

            }
            catch
            { }

        }

        private void lke_Chucvu_EditValueChanged(object sender, EventArgs e)
        {
            string a = lke_Chucvu.EditValue.ToString();
            lke_DonviBonhiem.Properties.DataSource = sv.getDMDVtheoLoaiCV(a);
        }

        private void btn_xoaAnh_Click(object sender, EventArgs e)
        {
           
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            //lke_dmDonvi.Enabled = false;
            lke_MaCB.Enabled = false;
            lke_Chucvu.Enabled = false;
            lke_DonviBonhiem.Enabled = false;
            txt_Quyetdinh.Enabled = false;
            de_Ngayky.Enabled = false;
            buttonEditFileQD.Enabled = false;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Frm_ThemDMDV frm = new Frm_ThemDMDV();
            frm.ShowDialog();
            //lke_dmDonvi.Properties.DataSource = sv.getDMDV();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            Frm_ThemDMDV frm = new Frm_ThemDMDV();
            frm.ShowDialog();
            //lke_dmDonvi.Properties.DataSource = sv.getDMDV();
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            
        }

        XtraFormPdfViewer fqd;
        private void buttonEditFileQD_Click(object sender, EventArgs e)
        {
            try
            {

                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File PDF(*.pdf)|*pdf";
                if (fdg.ShowDialog() == DialogResult.OK)
                {

                    buttonEditFileQD.Text = fdg.FileName;
                    if (fqd == null || fqd.IsDisposed)
                    {
                        fqd = new XtraFormPdfViewer(fdg.FileName);
                        fqd.Show();
                    }
                    else
                        fqd.Activate();
                }
                fdg = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }


        XtraFormPdfViewer frmpdf;
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                if ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[5]) != null && ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[5])).Length != 8000)
                    if (frmpdf == null || frmpdf.IsDisposed)
                    {

                        byte[] pdfbyte = (byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[5]);
                        //MessageBox.Show(pdfbyte.Length.ToString());
                        if (pdfbyte.Length != 0)
                        {
                            frmpdf = new XtraFormPdfViewer(pdfbyte);
                            frmpdf.Show();
                        }
                        else
                        {
                            frmpdf.Activate();
                        }
                    }
            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }




    }
}
